﻿using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HirelSeriesH
{
    public partial class EngineerUserListForm : UIForm
    {
        private string username;
        private DataTable dataTable;
        public EngineerUserListForm(string username)
        {
            this.username = username;
            InitializeComponent();
        }

        private void BtnCreateUser_Click(object sender, EventArgs e)
        {
            CreateEngineerUserForm createEngineerUserForm = new CreateEngineerUserForm(username);
            createEngineerUserForm.ShowDialog();
        }

        private void EngineerUserListForm_Load(object sender, EventArgs e)
        {
            DataSet dataSet = DBHelper.GetDataSet(DBSql.SELECT_ENGINEER_USER, 1);
            this.dataTable = dataSet.Tables[0];
            this.GridEngineerUser.AutoGenerateColumns = false;
            this.GridEngineerUser.DataSource = this.dataTable;
        }

        private void EngineerUserListForm_Activated(object sender, EventArgs e)
        {
            DataSet dataSet = DBHelper.GetDataSet(DBSql.SELECT_ENGINEER_USER, 1);
            this.dataTable = dataSet.Tables[0];
            this.GridEngineerUser.AutoGenerateColumns = false;
            this.GridEngineerUser.DataSource = this.dataTable;
        }

        private void BtnDeleUser_Click(object sender, EventArgs e)
        {
            DataGridViewRow dataGridViewRow = this.GridEngineerUser.CurrentRow;
            if (dataGridViewRow == null)
            {
                UIMessageDialog.ShowMessageDialog(this, "请先选择要删除的用户记录", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            else
            {
                if (!UIMessageDialog.ShowMessageDialog(this, "是否确定删除当前用户？", UILocalize.AskTitle, true, UIStyle.Colorful))
                {
                    return;
                }
                else
                {
                    int index = GridEngineerUser.CurrentRow.Index;
                    string userID = dataTable.Rows[index]["userID"].ToString();
                    MySqlParameter[]  paras = new MySqlParameter[] { new MySqlParameter("@ID", userID) };
                    //删除用户
                    DBHelper.ExecuteNonQuery(DBSql.DELE_HTRB_ENGINEER_USER, 1, paras);
                    //查询ENGINEER角色的ID
                    object result = DBHelper.ExecuteScalar(DBSql.SELECT_HTRB_ROLE_ENGINEER, 1);
                    string EngineerRoleID = result.ToString();
                    paras = new MySqlParameter[] { new MySqlParameter("@UserID", userID), new MySqlParameter("@RoleID", EngineerRoleID) };
                    //删除用户角色
                    DBHelper.ExecuteNonQuery(DBSql.DELE_HTRB_USER_ROLE_ENGINEER, 1, paras);
                    UIMessageDialog.ShowMessageDialog(this, "删除用户成功", UILocalize.InfoTitle, false, UIStyle.Colorful);
                }

            }

        }
    }
}
